package com.autologistics.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.autologistics.dao.DisparterDao;
import com.autologistics.dao.GoodsDao;
import com.autologistics.dao.GoodsorderDao;
import com.autologistics.dao.OrderTableDao;
import com.autologistics.model.Goods;
import com.autologistics.model.GoodsorderId;
import com.autologistics.model.Ordertable;
import com.autologistics.util.PageBean;

public class OrdertableServiceImpl implements OrdertableService{
	OrderTableDao ordertableDao;
	GoodsDao goodsDao;
	GoodsorderDao goodsorderDao;
	DisparterDao disparterDao;
	public GoodsDao getGoodsDao() {
		return goodsDao;
	}

	public DisparterDao getDisparterDao() {
		return disparterDao;
	}

	public void setDisparterDao(DisparterDao disparterDao) {
		this.disparterDao = disparterDao;
	}

	public void setGoodsDao(GoodsDao goodsDao) {
		this.goodsDao = goodsDao;
	}

	public GoodsorderDao getGoodsorderDao() {
		return goodsorderDao;
	}

	public void setGoodsorderDao(GoodsorderDao goodsorderDao) {
		this.goodsorderDao = goodsorderDao;
	}

	public OrderTableDao getOrdertableDao() {
		return ordertableDao;
	}

	public void setOrdertableDao(OrderTableDao ordertableDao) {
		this.ordertableDao = ordertableDao;
	}

	/**
	 * @return ������ or null
	 * @author cl
	 */
	public List<Ordertable> listAll(){
		return ordertableDao.listAll();
	}
	public int save(Ordertable orderTable){
		return ordertableDao.save(orderTable);
	}
	public Ordertable getByOrderID(String orderID){
		return ordertableDao.getByOrderID(orderID);
	}
	public Ordertable getById(int id){
		return ordertableDao.getById(id);
	}
	public void updata(Ordertable orderTable){
		ordertableDao.updata(orderTable);
	}
	public List<Ordertable> listAllByUser(int user){
		List<Ordertable> list=new ArrayList<Ordertable>();
		List<Goods> lg=goodsDao.listAll(user);
		for (Goods g :lg){
			List<GoodsorderId> listgoi=goodsorderDao.getGoodsorderByGoodsId(g.getId());
			for (GoodsorderId goi:listgoi){
				list.add(ordertableDao.getById(goi.getOrdertable().getId()));
			}
		}
		if (!list.isEmpty()){
			return list;
		}else{
			return null;
		}
	}
	public PageBean queryForOnePageUser(final int pageSize, int page,int allRow,int user){
		List<Ordertable> list1=new ArrayList<Ordertable>();
		List<Goods> lg=goodsDao.listAll(user);
		for (Goods g :lg){
			List<GoodsorderId> listgoi=goodsorderDao.getGoodsorderByGoodsId(g.getId());
			for (GoodsorderId goi:listgoi){
				list1.add(ordertableDao.getById(goi.getOrdertable().getId()));
			}
		}
		System.out.println("allRow    "+allRow);
		int totalPage = PageBean.countTotalPage(pageSize, allRow); //总页数  
		System.out.println("totalPage    "+totalPage);
		
		final int offset = PageBean.countOffset(pageSize, page); //当前页开始记录  
		System.out.println("offset    "+offset);
		List<Ordertable> list = new ArrayList<Ordertable>();
		if (offset+pageSize-1>allRow-1){
			list=list1.subList(offset, allRow);
		}else{
			list=list1.subList(offset, offset+pageSize);
		}
		final int length = pageSize; // 每页记录数    
		final int currentPage = PageBean.countCurrentPage(page); // 当前页    
		//把分页信息保存到Bean当中    
		PageBean pageBean  = new PageBean();    
		pageBean.setPageSize(pageSize);    
		pageBean.setCurrentPage(currentPage);    
		pageBean.setAllRow(allRow);    
		pageBean.setTotalPage(totalPage);    
		pageBean.setOrdertableList(list);    
		return pageBean; 
	}
	public PageBean queryForOnePage(final int pageSize, int page,int allRow){
		
		System.out.println("allRow    "+allRow);
		int totalPage = PageBean.countTotalPage(pageSize, allRow); //总页数  
		System.out.println("totalPage    "+totalPage);
		
		final int offset = PageBean.countOffset(pageSize, page); //当前页开始记录  
		System.out.println("offset    "+offset);
		final int length = pageSize; // 每页记录数    
		final int currentPage = PageBean.countCurrentPage(page); // 当前页    
		List<Ordertable> list=ordertableDao.queryForOnePage(offset, length);
		//把分页信息保存到Bean当中    
		PageBean pageBean  = new PageBean();    
		pageBean.setPageSize(pageSize);    
		pageBean.setCurrentPage(currentPage);    
		pageBean.setAllRow(allRow);    
		pageBean.setTotalPage(totalPage);    
		pageBean.setOrdertableList(list);    
		return pageBean; 
	}
	//--------------------------search---------------------------------
	public PageBean searchOrdertableId(final int pageSize, int page,String id){
		List<Ordertable> list2=ordertableDao.listAll();
		List<Ordertable> list1=new ArrayList<Ordertable>();
		for (Ordertable o:list2){
			if (o.getOrderId().equals(id)){
				list1.add(o);
			}
		}
		int allRow=list1.size();
		System.out.println("allRow    "+allRow);
		int totalPage = PageBean.countTotalPage(pageSize, allRow); //总页数  
		System.out.println("totalPage    "+totalPage);
		
		final int offset = PageBean.countOffset(pageSize, page); //当前页开始记录  
		System.out.println("offset    "+offset);
		List<Ordertable> list = new ArrayList<Ordertable>();
		if (offset+pageSize-1>allRow-1){
			list=list1.subList(offset, allRow);
		}else{
			list=list1.subList(offset, offset+pageSize);
		}
		final int length = pageSize; // 每页记录数    
		final int currentPage = PageBean.countCurrentPage(page); // 当前页    
		//把分页信息保存到Bean当中    
		PageBean pageBean  = new PageBean();    
		pageBean.setPageSize(pageSize);    
		pageBean.setCurrentPage(currentPage);    
		pageBean.setAllRow(allRow);    
		pageBean.setTotalPage(totalPage);    
		pageBean.setOrdertableList(list);    
		return pageBean; 
	}
	public PageBean searchOrdertableIdUser(final int pageSize, int page,String id,int user){
		List<Ordertable> list1=new ArrayList<Ordertable>();
		List<Goods> lg=goodsDao.listAll(user);
		for (Goods g :lg){
			List<GoodsorderId> listgoi=goodsorderDao.getGoodsorderByGoodsId(g.getId());
			for (GoodsorderId goi:listgoi){
				if (ordertableDao.getById(goi.getOrdertable().getId()).getOrderId().equals(id))
				list1.add(ordertableDao.getById(goi.getOrdertable().getId()));
			}
		}
		int allRow=list1.size();
		System.out.println("allRow    "+allRow);
		int totalPage = PageBean.countTotalPage(pageSize, allRow); //总页数  
		System.out.println("totalPage    "+totalPage);
		
		final int offset = PageBean.countOffset(pageSize, page); //当前页开始记录  
		System.out.println("offset    "+offset);
		List<Ordertable> list = new ArrayList<Ordertable>();
		if (offset+pageSize-1>allRow-1){
			list=list1.subList(offset, allRow);
		}else{
			list=list1.subList(offset, offset+pageSize);
		}
		final int length = pageSize; // 每页记录数    
		final int currentPage = PageBean.countCurrentPage(page); // 当前页    
		//把分页信息保存到Bean当中    
		PageBean pageBean  = new PageBean();    
		pageBean.setPageSize(pageSize);    
		pageBean.setCurrentPage(currentPage);    
		pageBean.setAllRow(allRow);    
		pageBean.setTotalPage(totalPage);    
		pageBean.setOrdertableList(list);    
		return pageBean; 
	}
	public PageBean searchDisparter(final int pageSize, int page,String name){
		List<Ordertable> list2=ordertableDao.listAll();
		for (Ordertable o:list2){
			o.setDisparter(disparterDao.getById(o.getDisparter().getId()));
		}
		List<Ordertable> list1=new ArrayList<Ordertable>();
		for (Ordertable o:list2){
			if (o.getDisparter().getName().contains(name)){
				list1.add(o);
			}
		}
		int allRow=list1.size();
		System.out.println("allRow    "+allRow);
		int totalPage = PageBean.countTotalPage(pageSize, allRow); //总页数  
		System.out.println("totalPage    "+totalPage);
		
		final int offset = PageBean.countOffset(pageSize, page); //当前页开始记录  
		System.out.println("offset    "+offset);
		List<Ordertable> list = new ArrayList<Ordertable>();
		if (offset+pageSize-1>allRow-1){
			list=list1.subList(offset, allRow);
		}else{
			list=list1.subList(offset, offset+pageSize);
		}
		final int length = pageSize; // 每页记录数    
		final int currentPage = PageBean.countCurrentPage(page); // 当前页    
		//把分页信息保存到Bean当中    
		PageBean pageBean  = new PageBean();    
		pageBean.setPageSize(pageSize);    
		pageBean.setCurrentPage(currentPage);    
		pageBean.setAllRow(allRow);    
		pageBean.setTotalPage(totalPage);    
		pageBean.setOrdertableList(list);    
		return pageBean; 
	}
	public PageBean searchDisparterUser(final int pageSize, int page,String name,int user){
		List<Ordertable> list1=new ArrayList<Ordertable>();
		
		List<Goods> lg=goodsDao.listAll(user);
		for (Goods g :lg){
			List<GoodsorderId> listgoi=goodsorderDao.getGoodsorderByGoodsId(g.getId());
			for (GoodsorderId goi:listgoi){
				Ordertable o=ordertableDao.getById(goi.getOrdertable().getId());
				o.setDisparter(disparterDao.getById(o.getDisparter().getId()));
				if (o.getDisparter().getName().contains(name))
				list1.add(ordertableDao.getById(goi.getOrdertable().getId()));
			}
		}
		int allRow=list1.size();
		System.out.println("allRow    "+allRow);
		int totalPage = PageBean.countTotalPage(pageSize, allRow); //总页数  
		System.out.println("totalPage    "+totalPage);
		
		final int offset = PageBean.countOffset(pageSize, page); //当前页开始记录  
		System.out.println("offset    "+offset);
		List<Ordertable> list = new ArrayList<Ordertable>();
		if (offset+pageSize-1>allRow-1){
			list=list1.subList(offset, allRow);
		}else{
			list=list1.subList(offset, offset+pageSize);
		}
		final int length = pageSize; // 每页记录数    
		final int currentPage = PageBean.countCurrentPage(page); // 当前页    
		//把分页信息保存到Bean当中    
		PageBean pageBean  = new PageBean();    
		pageBean.setPageSize(pageSize);    
		pageBean.setCurrentPage(currentPage);    
		pageBean.setAllRow(allRow);    
		pageBean.setTotalPage(totalPage);    
		pageBean.setOrdertableList(list);    
		return pageBean; 
	}
	public PageBean searchState(final int pageSize, int page,String num){
		List<Ordertable> list2=ordertableDao.listAll();
		List<Ordertable> list1=new ArrayList<Ordertable>();
		for (Ordertable o:list2){
			if (Integer.toString(o.getState()).equals(num)){
				list1.add(o);
			}
		}
		int allRow=list1.size();
		System.out.println("allRow    "+allRow);
		int totalPage = PageBean.countTotalPage(pageSize, allRow); //总页数  
		System.out.println("totalPage    "+totalPage);
		
		final int offset = PageBean.countOffset(pageSize, page); //当前页开始记录  
		System.out.println("offset    "+offset);
		List<Ordertable> list = new ArrayList<Ordertable>();
		if (offset+pageSize-1>allRow-1){
			list=list1.subList(offset, allRow);
		}else{
			list=list1.subList(offset, offset+pageSize);
		}
		final int length = pageSize; // 每页记录数    
		final int currentPage = PageBean.countCurrentPage(page); // 当前页    
		//把分页信息保存到Bean当中    
		PageBean pageBean  = new PageBean();    
		pageBean.setPageSize(pageSize);    
		pageBean.setCurrentPage(currentPage);    
		pageBean.setAllRow(allRow);    
		pageBean.setTotalPage(totalPage);    
		pageBean.setOrdertableList(list);    
		return pageBean; 
	}
	public PageBean searchStateUser(final int pageSize, int page,String num,int user){
		List<Ordertable> list1=new ArrayList<Ordertable>();
		for (Ordertable o:list1){
			o.setDisparter(disparterDao.getById(o.getDisparter().getId()));
		}
		List<Goods> lg=goodsDao.listAll(user);
		for (Goods g :lg){
			List<GoodsorderId> listgoi=goodsorderDao.getGoodsorderByGoodsId(g.getId());
			for (GoodsorderId goi:listgoi){
				Ordertable o=ordertableDao.getById(goi.getOrdertable().getId());
				if (Integer.toString(o.getState()).equals(num))
				list1.add(ordertableDao.getById(goi.getOrdertable().getId()));
			}
		}
		int allRow=list1.size();
		System.out.println("allRow    "+allRow);
		int totalPage = PageBean.countTotalPage(pageSize, allRow); //总页数  
		System.out.println("totalPage    "+totalPage);
		
		final int offset = PageBean.countOffset(pageSize, page); //当前页开始记录  
		System.out.println("offset    "+offset);
		List<Ordertable> list = new ArrayList<Ordertable>();
		if (offset+pageSize-1>allRow-1){
			list=list1.subList(offset, allRow);
		}else{
			list=list1.subList(offset, offset+pageSize);
		}
		final int length = pageSize; // 每页记录数    
		final int currentPage = PageBean.countCurrentPage(page); // 当前页    
		//把分页信息保存到Bean当中    
		PageBean pageBean  = new PageBean();    
		pageBean.setPageSize(pageSize);    
		pageBean.setCurrentPage(currentPage);    
		pageBean.setAllRow(allRow);    
		pageBean.setTotalPage(totalPage);    
		pageBean.setOrdertableList(list);    
		return pageBean; 
	}
	public PageBean searchTime(final int pageSize, int page,Timestamp time1,Timestamp time2){
		List<Ordertable> list2=ordertableDao.listAll();
		List<Ordertable> list1=new ArrayList<Ordertable>();
		for (Ordertable o:list2){
			if (o.getTime().after(time1) && o.getTime().before(time2)){
				list1.add(o);
			}
		}
		int allRow=list1.size();
		System.out.println("allRow    "+allRow);
		int totalPage = PageBean.countTotalPage(pageSize, allRow); //总页数  
		System.out.println("totalPage    "+totalPage);
		
		final int offset = PageBean.countOffset(pageSize, page); //当前页开始记录  
		System.out.println("offset    "+offset);
		List<Ordertable> list = new ArrayList<Ordertable>();
		if (offset+pageSize-1>allRow-1){
			list=list1.subList(offset, allRow);
		}else{
			list=list1.subList(offset, offset+pageSize);
		}
		final int length = pageSize; // 每页记录数    
		final int currentPage = PageBean.countCurrentPage(page); // 当前页    
		//把分页信息保存到Bean当中    
		PageBean pageBean  = new PageBean();    
		pageBean.setPageSize(pageSize);    
		pageBean.setCurrentPage(currentPage);    
		pageBean.setAllRow(allRow);    
		pageBean.setTotalPage(totalPage);    
		pageBean.setOrdertableList(list);    
		return pageBean; 
	}
	public PageBean searchTimeUser(final int pageSize, int page,Timestamp time1,Timestamp time2,int user){
		List<Ordertable> list1=new ArrayList<Ordertable>();
		for (Ordertable o:list1){
			o.setDisparter(disparterDao.getById(o.getDisparter().getId()));
		}
		List<Goods> lg=goodsDao.listAll(user);
		for (Goods g :lg){
			List<GoodsorderId> listgoi=goodsorderDao.getGoodsorderByGoodsId(g.getId());
			for (GoodsorderId goi:listgoi){
				Ordertable o=ordertableDao.getById(goi.getOrdertable().getId());
				if (o.getTime().after(time1) && o.getTime().before(time2))
				list1.add(ordertableDao.getById(goi.getOrdertable().getId()));
			}
		}
		int allRow=list1.size();
		System.out.println("allRow    "+allRow);
		int totalPage = PageBean.countTotalPage(pageSize, allRow); //总页数  
		System.out.println("totalPage    "+totalPage);
		
		final int offset = PageBean.countOffset(pageSize, page); //当前页开始记录  
		System.out.println("offset    "+offset);
		List<Ordertable> list = new ArrayList<Ordertable>();
		if (offset+pageSize-1>allRow-1){
			list=list1.subList(offset, allRow);
		}else{
			list=list1.subList(offset, offset+pageSize);
		}
		final int length = pageSize; // 每页记录数    
		final int currentPage = PageBean.countCurrentPage(page); // 当前页    
		//把分页信息保存到Bean当中    
		PageBean pageBean  = new PageBean();    
		pageBean.setPageSize(pageSize);    
		pageBean.setCurrentPage(currentPage);    
		pageBean.setAllRow(allRow);    
		pageBean.setTotalPage(totalPage);    
		pageBean.setOrdertableList(list);    
		return pageBean; 
	}
	public PageBean GoodsCount(final int pageSize, int page){
		List<Goods> list1=goodsDao.listAllGoods();
		
		int allRow=list1.size();
		System.out.println("allRow    "+allRow);
		int totalPage = PageBean.countTotalPage(pageSize, allRow); //总页数  
		System.out.println("totalPage    "+totalPage);
		
		final int offset = PageBean.countOffset(pageSize, page); //当前页开始记录  
		System.out.println("offset    "+offset);
		
		
		final int length = pageSize; // 每页记录数    
		List<Goods> list = ordertableDao.GoodsCount(offset, length);
		final int currentPage = PageBean.countCurrentPage(page); // 当前页    
		//把分页信息保存到Bean当中    
		PageBean pageBean  = new PageBean();    
		pageBean.setPageSize(pageSize);    
		pageBean.setCurrentPage(currentPage);    
		pageBean.setAllRow(allRow);    
		pageBean.setTotalPage(totalPage);    
		pageBean.setGoodsList(list);    
		return pageBean; 
	}
}
